import React, { Component } from "react";
import { history } from "umi";
class ErrorBoundary extends Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error) {
    return { hasError: true };
  }

  componentDidCatch(error, errorInfo) {
    // 在这里可以记录错误信息到日志服务
    console.error("Error caught by ErrorBoundary:", error, errorInfo);
  }

  render() {
    if (this.state.hasError) {
      // history.replace('/error')
      // 当捕获到错误时渲染备用 UI
      // return <h1>Something went wrong.</h1>;
    }

    // 正常情况下渲染子组件
    return this.props.children;
  }
}

export default ErrorBoundary;
